<template>
	<view class="page">
		<u-navbar :placeholder="true" title="创建小区" @leftClick="$utils.back()" border />
		<view class="body">
			<scroll-view :scroll-y="true" style="height: 100%;">
				<view class="viewWidth">
					<u--form labelWidth='100' labelPosition="top" :model="location"  ref="uForm">
						<u-form-item label="背景图:" borderBottom>
							<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
								:maxCount="1"></u-upload>
						</u-form-item>
						<u-form-item label="详细地址:"  borderBottom>
							<u--input :disabled="true" v-model="location.address" placeholder="请输入详细地址" />
						</u-form-item>
						<u-form-item label="小区名:" borderBottom>
							<u--input :disabled="true" v-model="location.name" placeholder="请输入小区名" />
						</u-form-item>
					</u--form>
				</view>
			</scroll-view>
		</view>
		<view class="viewWidth">
			<u-button text="创建" @click='add' :disabled="disabled"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				location: {
					cover: '',
					...uni.getStorageSync('location')
				},
				fileList1: [],
				disabled:false
			}
		},
		onLoad() {},
		methods: {
			add(){
				if(!this.location.cover){
					uni.$u.toast('请上传图片')
					return
				}
				if(this.disabled)return
				this.$api.District.Add(this.location).then(res=>{
					uni.$u.toast('上传成功')
					setTimeout(()=>{
						uni.reLaunch({
							url:'/Home/index'
						})
					},500)
				}).catch(err=>{
					this.disabled=false
					uni.$u.toast(err.data.message)
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.location.cover=null
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: this.$config.url + '/api/image/upload', //仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							'Content-Type': 'application/json;charset=UTF-8',
							'Authorization': "Bearer " + uni.getStorageSync('token')
						},
						success: (res) => {
							this.location.cover=JSON.parse(res.data).data.data.urlMedium
							resolve()
						},
						fail: (err) => {
							console.log(err);
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	.page {
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		margin-top: 20rpx;
	}
</style>